//
//  WelcomeViewController.swift
//  AAWeibo
//
//  Created by 上官风雨 on 15/11/13.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        view = backView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
       startAnimation()
    }
    
    //开始动画
    func startAnimation(){
        
        self.iconView.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 180)
        }
        //usingSpringWithDamping 弹簧系数 0~1 越小越弹
        //initialSpringVelocity加速度  
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        
                })
                
        }
        
    }
    
    func setupUI(){
        
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        
        //设置子控件的约束
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
            
        }
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        iconView.sd_setImageWithURL(UserAccountViewModel().imageUrl, placeholderImage: UIImage(named: "avatar_default_big"))

        welcomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            
        }
        
        
    }
    
    //头像的懒加载
    lazy var iconView : UIImageView = UIImageView()
    
    //背景图的懒加载
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //欢迎文字的懒加载
    lazy var welcomeLabel : UILabel = {
        
        let l = UILabel()
        
        l.textAlignment = .Center
        l.text = UserAccountViewModel().userName! + " 欢迎归来"
        
        l.font = UIFont.systemFontOfSize(19)
        l.sizeToFit()
        l.alpha = 0
        return l
        
    }()
    
}
